﻿#include "threadworker.h"
#include <QDebug>
#include <QtNetwork>

Controller::Controller() {
    worker = nullptr;
}

void Controller::setUpdateControlStart(QJsonObject value)
{
    if(worker){
        delete worker;
    }
    worker = new DataWorker(nullptr,this,value);

    worker->moveToThread(&workerThread);
    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
    connect(this, &Controller::sendData, worker, &DataWorker::Write);
    connect(this, &Controller::CloseTcp, worker, &DataWorker::CloseTcp);

    connect(this, &Controller::startInit, worker, &DataWorker::startInit);
//    connect(this, &Controller::startRequestData, worker, &DataWorker::requestDataValue);
//    connect(this, &Controller::startDownloadFile, worker, &DataWorker::startDownloadFile);
//    connect(this, &Controller::startUpdateNow, worker, &DataWorker::startUpdateNow);

//    connect(worker, &DataWorker::getData, this, &Controller::getData);
//    connect(worker, &DataWorker::canUpdate, this, &Controller::canUpdate);
//    connect(worker, &DataWorker::exitApp, this, &Controller::exitApp);
    workerThread.start();
    emit startInit();
}

void Controller::SocketStateChange(int type,QString value)
{
    emit sigSocketStateChange(type,value);
}

void Controller::MessageRev(int type,int sub,QString value)
{
    emit sigMessageRev(type,sub,value);
}

DataWorker::DataWorker(QObject *parent,Controller *control,QJsonObject data):
    QThread(parent), quit(false)
{
    pControl = control;
    m_server = data;
//    qDebug()<<"ConnectTcp:"<<data;

//    connect(&mWatcher,SIGNAL(fileChanged(QString)),this,SLOT(fileChanged(QString)));
//    mWatcher.addPath(QCoreApplication::applicationDirPath()+"/config/"+"temp.ini");
//    m_tempValue = getLocalInfo(QCoreApplication::applicationDirPath()+"/config/"+"temp.ini");
}

void DataWorker::startInit()
{
    qDebug()<<"ConnectTcp:"<<m_server;
    m_pserver = new serverthread(nullptr,m_server.value("ip").toString(),m_server.value("port").toInt());
    connect(m_pserver,SIGNAL(MessageRev(int,int,QString)),pControl,SLOT(MessageRev(int,int,QString)));
    connect(m_pserver,SIGNAL(SocketStateChange(int,QString)),pControl,SLOT(SocketStateChange(int,QString)));

    m_pserver->started();
}

void DataWorker::Write(QByteArray data)
{
    m_pserver->Write(data);
}

void DataWorker::CloseTcp()
{
    m_pserver->CloseTcp();
}

DataWorker::~DataWorker(){
    //qCDebug(webLog)<<"~WebServiceWorker:"<<m_pValue->m_id;
    if(m_pserver){
        m_pserver->CloseTcp();
        delete m_pserver;
        m_pserver = nullptr;
    }

    mutex.lock();
    quit = true;
    mutex.unlock();
    wait();
}

QStringList DataWorker::readSettings(QSettings *pSet,QString fileskey,QString filekey){
    QStringList result;
    const int count = pSet->beginReadArray(fileskey);
    for (int i = 0; i < count; ++i) {
        pSet->setArrayIndex(i);
        result.append(pSet->value(filekey).toString());
    }
    pSet->endArray();
    return result;
}


//void DataWorker::fileChanged(const QString &path)
//{
//    m_tempValue = getLocalInfo(path);
//}


void DataWorker::requestData(int type)
{
    QMutexLocker locker(&mutex);
//    m_type = type;
    if (!isRunning())
        start();
    else{
        wait(10000);
        start();
    }
}

void DataWorker::run()
{


}
